package com.woniu.user.service.impl;

import cn.hutool.core.convert.NumberWithFormat;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.common.dto.RechargeOrderDto;
import com.woniu.common.util.ResponseData;
import com.woniu.common.util.ResponseEnum;
import com.woniu.user.mapper.TUserMapper;
import com.woniu.user.pojo.TUser;
import com.woniu.user.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.woniu.common.constant.GlobalConstant.SALT;

/**
* @author 91211
* @description 针对表【t_user(用户表)】的数据库操作Service实现
* @createDate 2024-05-19 14:34:07
*/
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
    implements TUserService{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public ResponseData<TUser> login(String username, String pwd) {
        //根据用户名和密码查询数据
        //pwd = SecureUtil.md5(pwd);
        //加盐
        pwd = SecureUtil.md5(pwd + SALT);

        //根据用户名密码查数据
        TUser user = lambdaQuery().eq(TUser::getPhone, username)
                .eq(TUser::getPwd, pwd)
                .one();

        //如果查询到相关记录则生成jwt并且返回jwt,否则返回null
        if (ObjectUtils.isEmpty(user)){
            return new ResponseData<>().fail(ResponseEnum.LOGIN_FAIL);
        }

        //生成jwt
        Map<String,Object> map = new HashMap<>(1);

        //脱敏
        user.setPwd(null);


        map.put("user",user);

        String jwt = JWTUtil.createToken(map, "woniu".getBytes());

        //jwt放redis
        redisTemplate.opsForValue().set(jwt,jwt,30, TimeUnit.MINUTES);

        //可以向redis里面放该用户的已有的所有的path

        return new ResponseData<>().ok(jwt);
    }


    @Override
    public ResponseData<TUser> getByPhone(String phone) {

        TUser user = lambdaQuery().eq(TUser::getPhone, phone).one();

        return new ResponseData<>().ok(user);
    }


    @Override
    public ResponseData<TUser> updatePhoneBill(RechargeOrderDto rechargeOrderDto) {

        lambdaUpdate().setSql("phone_bill = phone_bill +" + rechargeOrderDto.getTotalPrice())
                .eq(TUser::getPhone,rechargeOrderDto.getPhone()).update();

        lambdaUpdate().setSql("integral = integral +" +rechargeOrderDto.getTotalPrice())
                .eq(TUser::getPhone,rechargeOrderDto.getPhone()).update();
        return new ResponseData<>().ok();
    }


    @Override
    public ResponseData<?> getAppUserInfo(String token) {
        if (token==null){
            return new ResponseData<>().fail(ResponseEnum.FAIL);
        }
        //解析token
        JWT jwt = JWTUtil.parseToken(token);
        Map user = (Map) jwt.getPayload().getClaim("user");
        Object o = user.get("id");
        long id = ((NumberWithFormat) o).longValue();
        //拿去用户id查询
        TUser userInfo = lambdaQuery().eq(TUser::getId, id).one();
        Map map = new HashMap<>(5);
        map.put("id",userInfo.getId());
        map.put("account",userInfo.getAccount());
        map.put("availableMoney",userInfo.getAvailableMoney());
        map.put("integral",userInfo.getIntegral());
        map.put("phoneBill",userInfo.getPhoneBill());
        map.put("phone",userInfo.getPhone());
        map.put("name",userInfo.getAccount());
        return new ResponseData<>().ok(map);
    }

    @Override
    public ResponseData<?> deletePhoneBill(String phone) {

        lambdaUpdate().setSql("integral = integral -150").eq(TUser::getPhone,phone).update();

        return new ResponseData<>().ok();
    }


}




